﻿
using BCGM.MedicalWisdom.IServices;
using BCGM.MedicalWisdom.Services.BASE;
//using BCGM.MedicalWisdom.IRepository.Base;
using BCGM.MedicalWisdom.Model.BCGM_PatientModel;
using BCGM.MedicalWisdom.Model.DTO;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using System.Linq;
using SqlSugar;
using BCGM.MedicalWisdom.IRepository;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Repository.BASE;

namespace BCGM.MedicalWisdom.Services
{
    public class patientServices : BaseServices<patient>, IpatientServices
    {
        private readonly IBaseRepository<patient> _dal;
        private readonly IBaseRepository<PatientInfoDTO> _DTOdal;
        private readonly IBaseRepository<PatientDiagnoseInfoDTO> _patientDiagnoseInfo;
        private readonly IBaseRepository<totalDTO> _totalDTOdal;
        private readonly IpatientRepository _ipatientRepository;

        public patientServices(IBaseRepository<patient> dal, IBaseRepository<PatientInfoDTO> dTOdal, IBaseRepository<PatientDiagnoseInfoDTO> patientDiagnoseInfo, IBaseRepository<totalDTO> totalDTOdal, IpatientRepository ipatientRepository)
        {
            _dal = dal;
            _DTOdal = dTOdal;
            _patientDiagnoseInfo = patientDiagnoseInfo;
            _totalDTOdal = totalDTOdal;
            _ipatientRepository = ipatientRepository;
        }


        //public (Task<List<PatientInfoDTO>>, int) GetPatient(Expression<Func<PatientInfoDTO, bool>> whereExpression, int pageIndex, int pageSize, SugarParameter[] parameters)
        //{
        //    string sql = $"SELECT a.PatientId,a.UserId,a.Pname,a.Phone,b.AddRess,count(c.PatientId) DiaCount,sum(c.Amount) Amount FROM Patient a LEFT JOIN AddRess b ON a.PatientId = b.PatientID LEFT JOIN Diagnose c ON a.PatientId = c.PatientId where (b.IsDefault=1 or b.AddRess is null)";
        //    string totalSql = "select count(*) total from (SELECT a.PatientId FROM Patient a LEFT JOIN AddRess b ON a.PatientId = b.PatientID LEFT JOIN Diagnose c ON a.PatientId = c.PatientId WHERE (b.IsDefault = 1 OR b.AddRess IS NULL) GROUP BY a.PatientId) p";
        //    if (!parameters.IsNullOrEmpty())
        //    {
        //        //sql = sql + $" and (a.PatientId={parameters} or a.Pname like '%{parameters}%')";
        //        sql = sql + $" and a.Pname like '%{parameters}%'";
        //    }
        //    sql = sql + $" GROUP BY a.PatientId limit {(pageIndex - 1) * pageSize},{pageSize}";
        //    Task<List<PatientInfoDTO>> patientInfoDTOs = _DTOdal.QuerySql(sql);

        //    int total = _ipatientRepository.GetTotal(totalSql);
        //    return (patientInfoDTOs, total);
        //}        
        /// <summary>
        /// 获取患者信息
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public async Task<PageModel<PatientInfoDTO>> GetPatient(Expression<Func<PatientInfoDTO, bool>> whereExpression, int pageIndex, int pageSize, SugarParameter[] parameters)
        {
            string sql = $"SELECT a.PatientId,a.UserId,a.Pname,a.Phone,b.AddRess,count(c.PatientId) DiaCount,sum(c.Amount) Amount FROM Patient a LEFT JOIN AddRess b ON a.PatientId = b.PatientID LEFT JOIN Diagnose c ON a.PatientId = c.PatientId where (b.IsDefault=1 or b.AddRess is null)";
            string totalSql = "SELECT a.PatientId FROM Patient a LEFT JOIN AddRess b ON a.PatientId = b.PatientID LEFT JOIN Diagnose c ON a.PatientId = c.PatientId WHERE (b.IsDefault = 1 OR b.AddRess IS NULL) GROUP BY a.PatientId";
            if (!parameters.IsNullOrEmpty())
            {
                //sql = sql + $" and (a.PatientId={parameters} or a.Pname like '%{parameters}%')";
                sql = sql + $" and a.Pname like '%{parameters}%'";
            }
            sql = sql + $" GROUP BY a.PatientId limit {(pageIndex - 1) * pageSize},{pageSize}";
            List<PatientInfoDTO> patientInfoDTOs = await _DTOdal.QuerySql(sql);
            List<PatientInfoDTO> patienttotalSql = await _DTOdal.QuerySql(totalSql);

            int total = patienttotalSql.Count();
            return new PageModel<PatientInfoDTO>()
            {
                data=patientInfoDTOs,
                dataCount = total
            };
        }
        /// <summary>
        /// 获取患者详情信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PageModel<PatientDiagnoseInfoDTO>> GetPatientInfo(int pageIndex, int pageSize, int id)
        {
            string sql = $"SELECT a.PatientId,c.DiagnoseTime,a.Pname,c.AddRess,c.Amount,c.Disease,d.Name,d.doctorgrade,e.Name Hname FROM Patient a LEFT JOIN Diagnose c ON a.PatientId = c.PatientId LEFT JOIN doctor d ON c.DoctorId = d.Id LEFT JOIN Hospital e ON d.Hospital = e.Id WHERE a.IsDeleted = 0 and c.IsDeleted = 0 and a.PatientId = {id} limit {(pageIndex - 1) * pageSize},{pageSize}";
            string totalSql = $"SELECT a.PatientId FROM Patient a LEFT JOIN Diagnose c ON a.PatientId = c.PatientId LEFT JOIN doctor d ON c.DoctorId = d.Id LEFT JOIN Hospital e ON d.Hospital = e.Id WHERE a.IsDeleted = 0 and c.IsDeleted = 0 and a.PatientId = {id}";
            List<PatientDiagnoseInfoDTO> patientInfoDTOs = await _patientDiagnoseInfo.QuerySql(sql);
            List<PatientDiagnoseInfoDTO> patienttotalSql = await _patientDiagnoseInfo.QuerySql(totalSql);

            int total = patienttotalSql.Count();
            return new PageModel<PatientDiagnoseInfoDTO>()
            {
                data = patientInfoDTOs,
                dataCount = total
            };
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        public async Task<patient> PatientLogging(string UserId, string UserPwd)
        {
            var user = (await base.Query(a => a.UserId == UserId && a.UserPwd == UserPwd)).FirstOrDefault();
            if (user != null && user.IsDeleted == false)
            {
                return user;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        public async Task<patient> PatientLogin(string UserId, string UserPwd)
        {
            var user = (await base.Query(a => a.UserId == UserId && a.UserPwd == UserPwd)).FirstOrDefault();
            if (user != null && user.IsDeleted == false)
            {
                return user;
            }
            else
            {
                return user;
            }
        }
    }
}